using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Framework.Library.Components;
using Framework.Library.Components.Providers;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace Framework.Library.Utilities.Database
{
    public class Info
    {
        static string key = "Framework.Portal.Infomation";
        public static DataTable GetVersion()
        {
            string param = "GetInfomation?t=version";
            DataCaching data = new DataCaching();
            DataTable dtCommands = (DataTable)data.GetHashCache(key, param);
            if (dtCommands != null) return dtCommands;
            dtCommands = DataAccessProvider.Instance().GetDatabaseInfo();
            data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire, dtCommands);
            return dtCommands;            
        }

        public static DataTable GetConfiguration()
        {
            string param = "GetInfomation?t=configuration";
            DataCaching data = new DataCaching();
            DataTable dtCommands = (DataTable)data.GetHashCache(key, param);
            if (dtCommands != null) return dtCommands;
            dtCommands = DataAccessProvider.Instance().GetDatabaseConfiguration();
            data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire, dtCommands);
            return dtCommands;
        }

        public static DataTable GetOwners()
        {
            string param = "GetOwners?t=owner";
            DataCaching data = new DataCaching();
            DataTable dtCommands = (DataTable)data.GetHashCache(key, param);
            if (dtCommands != null) return dtCommands;
            dtCommands = DataAccessProvider.Instance().GetOwners();
            data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire, dtCommands);
            return dtCommands;
        }

        public static DataTable GetFileGroups()
        {
            string param = "GetFileGroups?t=FileGroup";
            DataCaching data = new DataCaching();
            DataTable dtCommands = (DataTable)data.GetHashCache(key, param);
            if (dtCommands != null) return dtCommands;
            dtCommands = DataAccessProvider.Instance().GetFileGroups();
            data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire, dtCommands);
            return dtCommands;
        }

        public static DataTable GetDataTypes()
        {
            string param = "GetDataTypes?t=DataTypes";
            DataCaching data = new DataCaching();
            DataTable dtCommands = (DataTable)data.GetHashCache(key, param);
            if (dtCommands != null) return dtCommands;
            dtCommands = DataAccessProvider.Instance().GetDataTypes();
            data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire, dtCommands);
            return dtCommands;
        }

        public static DataTable GetDefaults()
        {
            string param = "GetDefaults?t=GetDefaults";
            DataCaching data = new DataCaching();
            DataTable dtCommands = (DataTable)data.GetHashCache(key, param);
            if (dtCommands != null) return dtCommands;
            dtCommands = DataAccessProvider.Instance().GetDefaults();
            data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire, dtCommands);
            return dtCommands;
        }

        public static DataTable GetRules()
        {
            string param = "GetRules?t=GetRules";
            DataCaching data = new DataCaching();
            DataTable dtCommands = (DataTable)data.GetHashCache(key, param);
            if (dtCommands != null) return dtCommands;
            dtCommands = DataAccessProvider.Instance().GetRules();
            data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire, dtCommands);
            return dtCommands;
        }

        public static void CheckDropList(DropDownList objDrop, Object strValue)
        {
            objDrop.Items[objDrop.SelectedIndex].Selected = false;
            //Find the value
            if (objDrop.Items.FindByValue(strValue.ToString()) != null)
            {
                //Select the value
                objDrop.Items.FindByValue(strValue.ToString()).Selected = true;
            }
        }
        public static void CheckRadioList(RadioButtonList objCheck, Object strValue)
        {
            objCheck.Items[objCheck.SelectedIndex].Selected = false;
            if (objCheck.Items.FindByValue(strValue.ToString()) != null)
            {
                objCheck.Items.FindByValue(strValue.ToString()).Selected = true;
            }
        }
        public static bool CheckSpecialChar(string stringToControl)
        {
            char[] trim = { '=', '\\', ';', ':', ',', '+', '*', '\'', ']', '[', '{', '}', ' ' }; //'.',
            if (stringToControl.IndexOfAny(trim) >= 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #region "Variables"

        /// <summary>
        /// Object Name
        /// </summary>
        private string _Name;
        /// <summary>
        /// Object Owner
        /// </summary>
        private string _Owner;        
        /// <summary>
        /// Object ID
        /// </summary>
        private long _ID;

        #endregion

        #region "Properties"

        /// <summary>
        /// Object Owner
        /// </summary>
        public string Owner
        {
            get
            {
                return _Owner;
            }
            set
            {
                _Owner = value;
            }
        }

        /// <summary>
        /// Object ID
        /// </summary>
        public long ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }

        /// <summary>
        /// Object name
        /// </summary>
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }       

        #endregion

        /// <summary>
        /// Return the complete object name.
        /// If the Name or the owner have some special chars, then the complete name
        /// will be [owner].[name] instead of owner.name
        /// </summary>
        /// <returns>Default Name</returns>
        public string GetObjectName()
        {
            string _ObjectName = "";
            if (CheckSpecialChar(_Name) || CheckSpecialChar(_Owner))
            {
                _ObjectName = "[" + _Owner + "].[" + _Name + "]";
            }
            else
            {
                _ObjectName = _Owner + "." + _Name;
            }
            return _ObjectName;
        }
    }
}
